<?php
namespace app\index\controller;
use app\index\controller\BaseController;

class NewsController extends BaseController {
	//电子杂志
	public function index() {
		cookie('URL', url());
		if (!cookie('USER')) {
			$this->redirect('Index/login');
		}
		if ($this->member['user_type'] == 1) {
			$this->error('请先到个人中心订阅情报站方再查看',url('member/dingyue'));
		}
		$catesno = db('news_cate')->where('status',1)->where('pid',0)->where('id', 'not in', $this->member['yuetype1'])->select();
		$this->assign('catesno', $catesno);
		$cates = db('news_cate')->where('status',1)->where('pid',0)->where('id', 'in', $this->member['yuetype1'])->select();
		$this->assign('cates', $cates);
		$where['cate_id1'] = $cates[0]['id'];
		$this->assign('cate_id1', $where['cate_id1']);
		$res = db('news')->field('id,add_time,cate_id1,thumb')->where($where)->group('add_time')->order('add_time desc')->page(1, 9)->select();
		$this->assign('res', $res);
		return $this->fetch();
	}
	//电子杂志
	public function ajax_index($page = 2) {
		if (!cookie('USER')) {
			$this->error('请先登录');
		}
		if ($this->member['user_type'] == 1) {
			$this->error('请先到个人中心订阅情报站方再查看');
		}
		$where['cate_id1'] = input('cate_id1');
		$news = db('news')->field('id,add_time,cate_id1,thumb')->where($where)->group('add_time')->order('add_time desc')->page($page, 9)->select();
		if ($news) {
			$this->success('获取成功', '', $news);
		} else {
			$this->error('没有数据了');
		}
		return $this->fetch();
	}
	//电子杂志
	public function dy_show($addtime='',$cate_id1='') {
		
		$where['cate_id1'] = $cate_id1;
		$where['add_time'] = $addtime;
		$where['type'] = 4;
		$this->assign('addtime', $addtime);
		
		$news = db('news')->field('id,title,thumb,type,clicks,add_time')->where($where)->order('id desc')->find();
		$this->assign('news', $news);
		
		$js = db('news')->field('id,title,thumb,type,clicks,add_time')->where($where)->order('id desc')->select();
		$this->assign('js', $js);
		
		$zc['type'] = 1;
		$zc['type'] = 3;
		$zc['add_time'] = $addtime;
		$zc = db('news')->field('id,title,thumb,type,clicks,add_time')->where($zc)->order('id desc')->select();
		$this->assign('zc', $zc);
		
		$jg['add_time'] = $addtime;
		$jg['cate_id1'] = $cate_id1;
		$jg = db('newsjg')->field('coutent',true)->where($jg)->order('update_time desc')->select();
		$this->assign('jg', $jg);
		
		$this->assign('cate_id1', $cate_id1);
		
		return $this->fetch();
	}
	
	//政策
	public function zcnews() {
		cookie('URL', url());
		if (!cookie('USER')) {
			$this->redirect('Index/login');
		}
		if ($this->member['user_type'] == 1) {
			$this->error('请先到个人中心订阅情报站方再查看',url('member/dingyue'));
		}
		$where['type'] = 3;
		$news = db('news')->field('content', true)->where($where)->order('sort desc,add_time desc')->page(1, 6)->select();
		$this->assign('news', $news);
		return $this->fetch();
	}
	
	public function ajax_zcnews($page = 2) {
		if (!cookie('USER')) {
			$this->error('请先登录');
		}
		if ($this->member['user_type'] == 1) {
			$this->error('请先到个人中心订阅情报站方再查看');
		}
		$where['type'] = 3;
		$news = db('news')->field('content', true)->where($where)->order('sort desc,add_time desc')->page($page, 6)->select();
		if ($news) {
			$this->success('获取成功', '', $news);
		} else {
			$this->error('没有数据了');
		}
	}
	//生活
	public function shnews() {
		cookie('URL', url());
		if (!cookie('USER')) {
			$this->redirect('Index/login');
		}
		if ($this->member['user_type'] == 1) {
			$this->error('请先到个人中心订阅情报站方再查看',url('member/dingyue'));
		}
		if(input('cate_id1')){
			$where['cate_id1'] = input('cate_id1');
			
		}else{
			$where['cate_id1'] = 1;
		}
		$this->assign('cate_id1', $where['cate_id1']);
		$where['type'] = 1;
		$news = db('news')->field('content', true)->where($where)->order('sort desc,add_time desc')->page(1, 6)->select();
		$this->assign('news', $news);
		return $this->fetch();
	}
	
	public function ajax_shnews($page = 2) {
		if (!cookie('USER')) {
			$this->error('请先登录');
		}
		if ($this->member['user_type'] == 1) {
			$this->error('请先到个人中心订阅情报站方再查看');
		}
		$where['cate_id1'] = input('cate_id1');
		$this->assign('cate_id1', $where['cate_id1']);
		$where['type'] = 1;
		$news = db('news')->field('content', true)->where($where)->order('sort desc,add_time desc')->page($page, 6)->select();
		if ($news) {
			$this->success('获取成功', '', $news);
		} else {
			$this->error('没有数据了');
		}
	}
	//技术
	public function jsnews() {
		cookie('URL', url());
		if (!cookie('USER')) {
			$this->redirect('Index/login');
		}
		if ($this->member['user_type'] == 1) {
			$this->error('请先到个人中心订阅情报站方再查看',url('member/dingyue'));
		}
		$cates = db('news_cate')->where('status',1)->where('id', 'in', $this->member['yuetype'])->select();
		$this->assign('cates', $cates);
		$where['cate_id2'] = $cates[0]['id'];
		$this->assign('cate_id2', $where['cate_id2']);
		$where['type'] = 4;
		$news = db('news')->field('content', true)->where($where)->order('sort desc,add_time desc')->page(1, 6)->select();
		$this->assign('news', $news);
		return $this->fetch();
	}
	
	public function ajax_jsnews($page = 2) {
		if (!cookie('USER')) {
			$this->error('请先登录');
		}
		if ($this->member['user_type'] == 1) {
			$this->error('请先到个人中心订阅情报站方再查看');
		}
		$cates = db('news_cate')->where('status',1)->where('id', 'in', $this->member['yuetype'])->select();
		$where['cate_id2'] = input('cate_id2', $cates[0]['id']);
		$this->assign('cate_id2', $where['cate_id2']);
		$where['type'] = 4;
		$news = db('news')->field('content', true)->where($where)->order('sort desc,add_time desc')->page($page, 6)->select();
		if ($news) {
			$this->success('获取成功', '', $news);
		} else {
			$this->error('没有数据了');
		}
	}
	//价格
	public function jgnews() {
		if (!cookie('USER')) {
			$this->error('请先登录');
		}
		if ($this->member['user_type'] == 1) {
			$this->error('请先到个人中心订阅情报站方再查看',url('member/dingyue'));
		}
		
		$cates = db('news_cate')->where('status',1)->where('id', 'in', $this->member['yuetype1'])->order('sort desc')->select();
		$this->assign('cates', $cates);
		$where['cate_id1'] = $cates[0]['id'];
		$this->assign('cate_id1', $where['cate_id1']);
		$news = db('newsjg')->where($where)->order('add_time desc')->page(1, 6)->select();
		$this->assign('news', $news);
		
		return $this->fetch();
	}
	
	public function ajax_jgnews($page = 2,$cate_id1) {
		$where['cate_id1'] = $cate_id1;
		$news = db('newsjg')->where($where)->order('add_time desc')->page($page, 6)->select();
		
		if ($news) {
			$this->success('获取成功', '', $news);
		} else {
			$this->error('没有数据了');
		}
	}


	public function news_show($id) {
		if (!is_numeric($id)) {
			$this->error('参数错误');
		}
		cookie('URL', url('news/news_show', array('id' => $id)));
		$data = db('news')->where('id', $id)->find();
		if(cookie('USER')['user_type'] == 1){
			$data['content'] = "<a href=".url('member/dingyue').">点击订阅</a>,查看内容需要订阅";
		}
		db('news')->where('id', $id)->setInc('clicks');
		$this->assign('data', $data);
		
		return $this->fetch();
	}
	
	public function news_show1($id) {
		if (!is_numeric($id)) {
			$this->error('参数错误');
		}
		cookie('URL', url('news/news_show', array('id' => $id)));
		$data = db('news')->where('id', $id)->find();
		if(cookie('USER')['user_type'] == 1){
			$data['content'] = "<a href=".url('member/dingyue').">点击订阅</a>,查看内容需要订阅";
		}
		
		db('news')->where('id', $id)->setInc('clicks');
		$this->assign('data', $data);
		return $this->fetch();
	}

	public function jg_show($id) {
		if (!is_numeric($id)) {
			$this->error('参数错误');
		}
		$data = db('newsjg')->where('id', $id)->find();
		$this->assign('data', $data);
		$list = db('newsjg')->field('pprice,add_time')->where('cate_id2', $data['cate_id2'])->where('market', $data['market'])->order('add_time desc')->limit(7)->select();
		foreach ($list as $value) {
			 $array[] = $value['pprice'];
			 $array1[] = substr($value['add_time'],8,10);
		}
		$this->assign('list', join(",",$array));
		$this->assign('list1', join(",",$array1));
		return $this->fetch();
	}

}
